﻿using Genius.Brain.Shared.Strategy;
using Modules.VocabularyTrainer.Shared.BO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Modules.VocabularyTrainer.Shared.Locator;

namespace Modules.VocabularyTrainer.Impl.Strategy
{
    class DictionaryDatabaseRecordJsonConverterStrategy : IJsonConverterStrategy<IDictionaryDatabaseRecord>
    {
        public IDictionaryDatabaseRecord ConvertFromJson(JToken rec)
        {

            var d = VocabularyTrainerLocator.GetDictionaryDatabaseRecord();
            d.Text = new Dictionary<string, string>();
            foreach (var r in rec.Children<JProperty>())
            {
                var name = r.Name;
                var value = r.Value;
                switch(name)
                {
                    case "list":
                        d.List = value.Value<string>();
                        break;
                    case "level":
                        d.Level = value.Value<int>();
                        break;
                    default:
                        d.Text[name] = value.Value<string>();
                        break;
                }
            }

            return d;
        }
    }
}
